package com.fourth_groups.shopping121store.ui.person.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBar;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.fourth_groups.shopping121store.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

public class PeopleActivity extends AppCompatActivity  {
    private PopupWindow window;
    private View contentView;
    private LinearLayout linear_titlt;
    private TextView poup_titlt;
    private Button poup_camero;
    private Button poup_camero1;
    private ImageView people_ivg;
    private static final int CAMERA_REQUEST = 1888;
    private LinearLayout nickname;
    private TextView person_nickname;
    private LinearLayout linear_sex;
    private LinearLayout linear_phone;
    private LinearLayout linear_password;
    private LinearLayout adress;
    private LinearLayout third;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_people);
        EventBus.getDefault().register(this);
        initview();

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getStringnick(String nick){
        person_nickname.setText(nick);
    }

    private void initview() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.popup_layout, null);
        window = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, 100);
        linear_titlt = (LinearLayout) findViewById(R.id.linear_title);
        linear_sex = (LinearLayout) findViewById(R.id.linerlayout_sex);
        linear_phone = (LinearLayout) findViewById(R.id.linear_phone);
        linear_password = (LinearLayout) findViewById(R.id.linear_password);
        poup_titlt = (TextView) findViewById(R.id.poup_title);
        poup_camero = (Button) findViewById(R.id.poup_camero);
        poup_camero1 = (Button) findViewById(R.id.poup_camero1);
        people_ivg = (ImageView) findViewById(R.id.people_title);
        person_nickname = (TextView) findViewById(R.id.person_nickname);
        nickname = (LinearLayout) findViewById(R.id.linear_nickname);
        adress = (LinearLayout) findViewById(R.id.adress);
        third = (LinearLayout) findViewById(R.id.third);
        adress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(PeopleActivity.this, "功能暂未实现", Toast.LENGTH_SHORT).show();
            }
        });
        third.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(PeopleActivity.this, "小编正在努力中", Toast.LENGTH_SHORT).show();
            }
        });
        linear_titlt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.popup_layout,null);
                final PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT, true);
                popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                popupWindow.setOutsideTouchable(true);
                popupWindow.setFocusable(true);
                //获取屏幕宽度
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                popupWindow.setWidth(dm.widthPixels);
                //popupWindow.setAnimationStyle(R.style.popuwindow);
                //显示位置
                popupWindow.showAtLocation(view, Gravity.CENTER_VERTICAL, 0, 0);
                popupWindow.setOnDismissListener(new poponDismissListener());
                switch (v.getId()){
                    case R.id.poup_camero:
                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);
                        break;
                }

            }
        });
        linear_sex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.popup_sex,null);
                PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT, true);
                popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                popupWindow.setOutsideTouchable(true);
                popupWindow.setFocusable(true);
                //获取屏幕宽度
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                popupWindow.setWidth(dm.widthPixels);
                //popupWindow.setAnimationStyle(R.style.popuwindow);
                //显示位置
                popupWindow.showAtLocation(view, Gravity.CENTER_VERTICAL, 0, 0);
                popupWindow.setOnDismissListener(new poponDismissListener());
            }
        });
    nickname.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(PeopleActivity.this,Nickname.class);
            startActivity(intent);
        }
    });
        linear_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(PeopleActivity.this,PhoneActivity.class);
                startActivity(intent1);
            }
        });
        linear_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2=new Intent(PeopleActivity.this,PasswordActivity.class);
                startActivity(intent2);
            }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==CAMERA_REQUEST&&resultCode==RESULT_OK){
            Bitmap photo   = (Bitmap) data.getExtras().get("data");
            people_ivg.setImageBitmap(photo);
        }
    }

    private class poponDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {

        }
    }
    //获取照相机对象
    public Camera getCameraInstance() {
        Camera camera = Camera.open(0);
        if (camera != null) {
            return camera;
        }
        return null;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (window!=null&&window.isShowing()){
            window.dismiss();
            window=null;
        }
        return super.onTouchEvent(event);
    }
}
